Dreamweaver Article

How to Develop an Online Shopping Cart (Part Three)


Table of Contents

  1. Introduction
  2. Example Application Setup and the Database
  3. Application.cfm
  4. The Home Page
  5. The Check Out Page
  6. The Process Order Page
  7. The Administration Page

Index.cfm—The Shopping Cart Home Page

Index.cfm is the home page and where you display what you have to sell and allow users to add items to their cart. Open index.cfm in the Code view, and look at line 18.

<cfinclude template="inc_DevNetCart.cfm">

Here, include the inc_DevNetCart.cfm file I just discussed. Using SSIs (server-side includes) is a great way to separate functionality from presentation code. You can add this cart function to many pages across the application, and if you need to change something in the cart code, you only need to do it once on the included page. As a result, every page in the application that uses the included page will be automatically updated.

Next on the index.cfm page, I have a standard query that retrieves the cart items from the database and displays the products in a standard HTML table. There are a couple of things to point out here On line 43 of index.cfm, notice another server-side include. This calls a file that will show the user what is in their cart if a cart session exists. We will look at that file in a minute. Next, from lines 48 to 84 see that the cfoutput tag wraps a table row as shown in the code snippet below.

<cfoutput query="getItems">
<tr> 
<td width="36%"> 
<!--- The image file name is dynamically populated from the Items table in the database. ---> 
<img src="images/#getItems.itemImage#">
</td> 
<td width="64%"><table width="90%" border="0" align="center" cellpadding="2" cellspacing="2"> 
<tr> 
<td class="subhead">#getItems.itemName#</td> 
</tr> 
<tr> 
<td>#getItems.itemDiscrip# </td>
</tr> 
<tr> 
<td> 
<form name="form" method="post" action="#variables.ThisPage#"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td width="25%">Price: #LSCurrencyFormat(getItems.itemPrice,'local')#</td> 
<td width="25%" align="center"><input name="Qty" type="text" id="Qty" value="1" size="4"></td> 
<td width="50%" align="center"><input name="ItemID" type="hidden" id="ItemID" value="#getItems.itemID#"> 
<input name="Submit" type="submit" class="buttons" value="add to cart">
</td> 
</tr> 
</table> 
</form> 
</td> 
</tr> 
</table> 
</td> 
</tr> 
<tr> 
<td colspan="2"> 
<hr> 
</td> 
</tr> 
</cfoutput>

Doing this creates a loop that will generate one table row for each record found in the Items table in the database. This is known as a Repeat Region server behavior in Dreamweaver MX and you can find it in the Server Behavior tab of the Application panel group.

In this table row, I display the item data, and the image. Notice that the image name is generated dynamically by using the file name stored in the database. I also have a form that allows the user to enter a quantity and add an item to his cart.

inc_DisplayDevNetCart.cfm

Now, open the inc_DisplayDevNetCart.cfm file in the Code view. This file contains the code you use to show what a user has in his cart. This code is contained in a cfif tag that keeps the code from executing if no cart session exists. The cfif tag causes the code to execute if a cart session exists. Again, notice that I use an server-side include to reuse this code throughout the site. Includes are a great tool for reusing code and simplifying your site maintenance duties.

Next, I'll take you through the shopping cart's checkout page.